﻿using GDGeek;
using MrPP.SocketSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace MrPP.SerriedCabinet
{
    public class RespAreaColumnConfiguration : GDGeek.Singleton<RespAreaColumnConfiguration>, Resp
    {

        public Action<Data> onRecevie {
            get;
            set;
        }

        [Serializable]
        public class Column {
            public string name;
            public byte area;
            public byte index;
            public byte type;
            public byte layers;
            public string left;
            public string right;
        }
        [Serializable]
        public class Area
        {
            public string name;
            public byte director;
            public List<Column> columns = new List<Column>();
        }
        [Serializable]
        public class Room {
            public string name;
            public byte index;

            public float getTemperature()
            {
                return (float)(temperature) / 10f;
            }
            public float getHumidity()
            {

                return (float)(humidity);
            }
            public short temperature;
            public byte humidity;
            public List<Area> areas = new List<Area>();


        }
        [Serializable]
        public class Data{
            public List<Room> rooms = new List<Room>();
        }
        void Start() {
            ProtoManager.Instance.addResp(this);
        }
        void OnDestroy() {
            if (ProtoManager.IsInitialized) { 
                ProtoManager.Instance.removeResp(this);
            }
        }
        public void deserialize(DataStream reader)
        {
            ushort count = reader.ReadInt16();
            Data data = new Data();
            for (int i=0;  i<count; ++i){
                
                data.rooms.Add(readRoom(reader));
            }
            Debug.Log(JsonUtility.ToJson(data));
            GlobalConfigure cfg = this.gameObject.AskComponent<GlobalConfigure>();
            cfg.data = data;
            onRecevie?.Invoke(data);
            Debug.Log("rwader" + count);
        }

        private Room readRoom(DataStream reader)
        {
            Debug.Log("!!!");
          //  string name reader.ReadString8();
            Room room = new Room();
         
            room.name = reader.ReadString16();
            room.index = reader.ReadByte();
            room.temperature = reader.ReadSInt16();
            room.humidity = reader.ReadByte();

            ushort count = reader.ReadInt16();
            for (int i = 0; i < count; ++i) {
                room.areas.Add(readArea(reader));
            }
            Debug.Log(room.name);   
            return room;

        }

        private Area readArea(DataStream reader)
        {
            Area area = new Area();
            area.name = reader.ReadString16();
            Debug.Log(area.name);
            area.director = reader.ReadByte();

            ushort count = reader.ReadInt16();
            Debug.Log(count);
            
            for (int i = 0; i < count; ++i) {
                area.columns.Add(readColumn(reader));
            }
             
            return area;
        }

        private Column readColumn(DataStream reader)
        {
            Column column = new Column();
            column.name = reader.ReadString16();
            column.area = reader.ReadByte();
            column.index = reader.ReadByte();
            column.type = reader.ReadByte();
            column.layers = reader.ReadByte();
            column.left = reader.ReadString16();
            column.right = reader.ReadString16();
      
            return column;
        }

        public short getCommand()
        {
            var bytes = new byte[] { 0xB0, 0x00 };
            return BitConverter.ToInt16(bytes, 0);
        }

        public void serialize(DataStream writer)
        {
            Debug.Log("writer");
        }
    }
}